#include "personindex.h"

//清空m_layoutMain原布局内的所有元素
void rmWidget(QVBoxLayout *m_layoutMain)
{
    QLayoutItem *child;
    while ((child = m_layoutMain->takeAt(0)) != nullptr) {
        if(child->widget()) {
            child->widget()->setParent(nullptr);//setParent(nullptr),防止删除之后界面不消失
            delete child->widget();//释放
        }
        delete child;
    }
}

PersonIndex::PersonIndex(QWidget *parent): QWidget(parent)
{
    // 左侧博客信息
    blogInfo = new BlogInfo();
    QHBoxLayout *hLayout = new QHBoxLayout();
    blogInfo->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    hLayout->addWidget(blogInfo);

    // 右侧博客正文，或者博列表
    QWidget *widget = new QWidget(this);
    vLayout = new QVBoxLayout();
    widget->setLayout(vLayout);
    hLayout->addWidget(widget);

    setLayout(hLayout);
}
// 更改作者名字
void PersonIndex::setAuthorName(QString name)
{
    blogInfo->setAuthorName(name);
}
// 更改作者头像
void PersonIndex::setAuthorHead(QString path)
{
    blogInfo->setAuthorHead(path);
}

void PersonIndex::setOtherInfo(QString info1, QString info2, QString info3)
{
    blogInfo->setOtherInfo(info1, info2, info3);
}
// 点赞
void PersonIndex::setPraise(bool isPraise, QString p)
{
    sblog->setPraise(isPraise, p);
}
// 收藏
void PersonIndex::setCollection(bool isCollection, QString c)
{
   sblog->setCollection(isCollection, c);
}
// 评论
void PersonIndex::setComment(QString c)
{
    sblog->setComment(c);
}
QString PersonIndex::getPraise()
{
    return sblog->getPraise();
}
QString PersonIndex::getCollection()
{
    return sblog->getCollection();
}
QString PersonIndex::getComment()
{
    return sblog->getComment();
}
void PersonIndex::removeRight()
{
    rmWidget(vLayout);
}

// 文章列表
void PersonIndex::addBlogItem(QMap<QString, QString> map)
{
    // 创建item
    BlogItem *item = new BlogItem(this, &map);
    // 将item加入到垂直布局
    vLayout->addWidget(item);
    // 保存item到list
    itemList<<item;
    item->setMaximumHeight(200);
    // 绑定信号
    connect(item, SIGNAL(labelClicked(QMap<QString, QString>, int)), this, SLOT(slotBlogInfo(QMap<QString, QString>, int)));
}

void PersonIndex::addCommentItem(QMap<QString, QString> map)
{
    QWidget *comWidget = new QWidget();

    QWidget *widget1 = new QWidget();
    QHBoxLayout *hLayout1 = new QHBoxLayout();
    hLayout1->setMargin(0);
    QLabel *label1 = new QLabel(map["comment_user"]+"    ");
    QLabel *label2 = new QLabel(map["comment_time"]+"    ");
    QLabel *label3 = new QLabel(QString("评论了你的文章    "));
    QLabel *label4 = new QLabel(map["blog_name"]);
    hLayout1->addWidget(label1);
    hLayout1->addWidget(label2);
    hLayout1->addWidget(label3);
    hLayout1->addWidget(label4);
    QSpacerItem *spacer = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Fixed);
    hLayout1->addItem(spacer);
    widget1->setLayout(hLayout1);

    QVBoxLayout *vLayout1 = new QVBoxLayout();
    vLayout1->setMargin(0);
    vLayout1->addWidget(widget1);
    QLabel *label5 = new QLabel(map["comment_content"]);
    vLayout1->addWidget(label5);
    QLabel *label6 = new QLabel();
    label6->setMinimumHeight(2);
    label6->setMaximumHeight(2);
    label6->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
    vLayout1->addWidget(label6);
    comWidget->setLayout(vLayout1);

    vLayout->addWidget(comWidget);
}

void PersonIndex::addResItem(QMap<QString, QString> mapInfo)
{
    QMap<QString, QString> map = mapInfo;
    qint64 fsize = map["file_size"].toInt();
    if(fsize < 1024)
        map["file_size"] = QString::number(fsize)+" B";
    else if(fsize<1024*2024 && fsize>1024)
        map["file_size"] = QString::number(fsize/1024)+" KB";
    else if(fsize>1024*2024 && fsize<1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024))+" MB";
    else if(fsize>1024*2024*1024 && fsize<1024*1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024*1024))+" GB";

    // 创建控件
    ShowRes *res = new ShowRes(this, &map);
    // 绑定信号
    connect(res, SIGNAL(signalDownloadRes(QMap<QString, QString>)), this, SLOT(slotDownloadRes(QMap<QString, QString>)));
    connect(res, SIGNAL(signalUserRes(QString)), this, SLOT(slotUserRes(QString)));
    // 将控件加入到布局
    vLayout->addWidget(res);
}

// 显示文章
void PersonIndex::showBlog(QMap<QString, QString> mapInfo, int type)
{
    blogUser = mapInfo["blog_user"];
    blogStamp = mapInfo["blog_stamp"];
    // 删除其他控件
    rmWidget(vLayout);
    // 创建控件
    sblog = new ShowBlog(this, &mapInfo, type);
    // 绑定信号
    connect(sblog, SIGNAL(labelClicked(QMap<QString, QString>, QString)), this, SLOT(slotBlogOperation(QMap<QString, QString>, QString)));
    // 将控件加入到布局
    vLayout->addWidget(sblog);
}
// 发布文章
void PersonIndex::publishBlog()
{
    // 删除其他控件
    rmWidget(vLayout);
    // 创建控件
    PublishBlog *blog = new PublishBlog();
    // 绑定信号
    connect(blog, SIGNAL(signalPublishBlog(QMap<QString, QString>, QList<QString>)), this, SLOT(slotPublishBlog(QMap<QString, QString>, QList<QString>)));
    // 将控件加入到布局
    vLayout->addWidget(blog);
}

// 显示资源
void PersonIndex::showRes(QMap<QString, QString> mapInfo)
{
    QMap<QString, QString> map = mapInfo;
    qint64 fsize = map["file_size"].toInt();
    if(fsize < 1024)
        map["file_size"] = QString::number(fsize)+" B";
    else if(fsize<1024*2024 && fsize>1024)
        map["file_size"] = QString::number(fsize/1024)+" KB";
    else if(fsize>1024*2024 && fsize<1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024))+" MB";
    else if(fsize>1024*2024*1024 && fsize<1024*1024*1024*1024)
        map["file_size"] = QString::number(fsize/(1024*1024*1024))+" GB";
    // 删除其他控件
    rmWidget(vLayout);
    // 创建控件
    ShowRes *res = new ShowRes(this, &map);
    // 绑定信号
    connect(res, SIGNAL(signalDownloadRes(QMap<QString, QString>)), this, SLOT(slotDownloadRes(QMap<QString, QString>)));
    connect(res, SIGNAL(signalUserRes(QString)), this, SLOT(slotUserRes(QString)));
    // 将控件加入到布局
    vLayout->addWidget(res);
}
// 发布资源
void PersonIndex::publishRes()
{
    // 删除其他控件
    rmWidget(vLayout);
    // 创建控件
    PublishRes *res = new PublishRes();
     // 绑定信号
    connect(res, SIGNAL(signalPublishRes(QMap<QString, QString>)), this, SLOT(slotPublishRes(QMap<QString, QString>)));
    // 将控件加入到布局
    vLayout->addWidget(res);
}

void PersonIndex::showComment(QMap<int, QMap<QString, QString>> map)
{
    commentWidget = new QWidget();
    commentLayout = new QVBoxLayout();
    commentText = new QTextEdit();

    commentText->setMaximumHeight(50);
    commentText->setMinimumHeight(50);
    commentLayout->addWidget(commentText);

    QPushButton *btn = new QPushButton("发送");
    connect(btn, SIGNAL(clicked()), this, SLOT(slotComment()));
    commentLayout->addWidget(btn);

    QMap<int, QMap<QString, QString>>::iterator it;
    for(it=map.begin(); it!=map.end(); ++it)
    {
        QLabel *label1 = new QLabel((it.value())["comment_user"]+" : ");
        QLabel *label2 = new QLabel((it.value())["comment_time"]);
        QHBoxLayout *layout1 = new QHBoxLayout();
        layout1->setMargin(0);
        QWidget *widget1 = new QWidget();
        layout1->addWidget(label1);
        layout1->addWidget(label2);
        widget1->setLayout(layout1);
        commentLayout->addWidget(widget1);
        QLabel *label3 = new QLabel((it.value())["comment_content"]);
        commentLayout->addWidget(label3);
    }

    commentWidget->setLayout(commentLayout);
    vLayout->addWidget(commentWidget);
}

void PersonIndex::appendComment(QMap<int, QMap<QString, QString>> map)
{
    QMap<int, QMap<QString, QString>>::iterator it;
    for(it=map.begin(); it!=map.end(); ++it)
    {
        QLabel *label1 = new QLabel((it.value())["comment_user"]+" : ");
        QLabel *label2 = new QLabel((it.value())["comment_time"]);
        QHBoxLayout *layout1 = new QHBoxLayout();
        layout1->setMargin(0);
        QWidget *widget1 = new QWidget();
        layout1->addWidget(label1);
        layout1->addWidget(label2);
        widget1->setLayout(layout1);
        commentLayout->addWidget(widget1);
        QLabel *label3 = new QLabel((it.value())["comment_content"]);
        commentLayout->addWidget(label3);
    }
}

void PersonIndex::removeComment()
{
    rmWidget(commentLayout);
}

void PersonIndex::slotComment()
{
    emit signalComment(commentText->toPlainText());
}

// 点击某个文章的收藏、点赞、评论等操作
void PersonIndex::slotBlogOperation(QMap<QString, QString> map, QString msg)
{
    emit signalBlogClicked(map, msg);
}

// 从一堆文章列表中点中某个文章
void PersonIndex::slotBlogInfo(QMap<QString, QString> map, int type)
{
    emit signalBlogClicked(map, type);
}
// 发布文章，信号传递到上层
void PersonIndex::slotPublishBlog(QMap<QString, QString> map, QList<QString> list)
{
    emit signalPublishBlog(map, list);
}
// 下载资源，信号传递到上层
void PersonIndex::slotDownloadRes(QMap<QString, QString> map)
{
    emit signalDownloadRes(map);
}
void PersonIndex::slotUserRes(QString user)
{
    emit signalUserRes(user);
}
// 发布资源，信号传递到上层
void PersonIndex::slotPublishRes(QMap<QString, QString> map)
{
    emit signalPublishRes(map);
}

PersonIndex::~PersonIndex()
{

}
